/*
 * display_hal.c
 *
 * Created: 8/16/2013 3:25:47 PM
 *  Author: Ken Arok
 *
 *      Revision history:
 *      ---------------------------------------------------------
 *      Date			|	Revised by		|	Description
 *      ---------------------------------------------------------
 *      1. Aug 16, 2013	|	Yosef			| New establishment
 *
 *
 * \file
 *
 * \brief Hardware Abstraction Layer of LCD Display.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include "config_board.h"

#if BOARD_1_0_USED

#define LMB204BFC_LCDMODULE_USED	1



#if LMB204BFC_LCDMODULE_USED

#include "idefs.h"
#include "board.h"
#include "gpio.h"
#include "st7066u.h"
#include "delay.h"
#include "display_hal.h"


void display_init(void)
{
	st7066u_init();
}


void display_set_pos(uint8_t _row, uint8_t _col)
{
	if((_col > 0) && (_col < 21)) {
		switch(_row)
		{
			case 1:
				st7066u_send_instruction((const uint8_t)(0x80 + _col - 1));
			break;

			case 2:
				st7066u_send_instruction((const uint8_t)(0xC0 + _col - 1));
			break;

			case 3:
				st7066u_send_instruction((const uint8_t)(0x94 + _col - 1));
			break;

			case 4:
				st7066u_send_instruction((const uint8_t)(0xD4 + _col - 1));
			break;
			
			default: break;
		}		
	}

}


void display_put(uint8_t _row, uint8_t _col, const char _char)
{
	if(_col > 20) {
		_col = 1;
		_row++;
	}
	
	if((_row > 0) && (_row < 5)) {
		if(_col) {
			display_set_pos(_row, _col);
			st7066u_send_data((const uint8_t)_char);
		}
	}
}


void display_print(uint8_t _row, uint8_t _col, const char *_string)
{
	const uint8_t *_ptr = (const uint8_t *)_string;
	
	if(_col > 20) {
		_col = 1;
		_row++;
	}
	
	if((_row < 5) && (_col)) {
		display_set_pos(_row, _col);
		while(*_ptr && (_row < 5)) {
			st7066u_send_data(*_ptr);
			_ptr++;
			_col++;
			
			if(_col > 20) {
				_col = 1;
				_row++;
			}
		}
	}
}


void display_clear(void)
{
	const uint8_t _instr = 0x01;
	
	st7066u_send_instruction(_instr);
	delay_ms(5);
}


void display_control(const uint8_t _ctrl)
{
	st7066u_send_instruction(_ctrl);
	delay_ms(5);
}


void display_turn_backlight(uint8_t _on_off)
{
	switch(_on_off) {
		case iON:
			gpio_set_pin_high(LCD_BLON_GPIO);
		break;

		case iOFF:
			gpio_set_pin_low(LCD_BLON_GPIO);
		break;

		default: break;
	}
}


#elif XXXXXXX_LCDMODULE_USED

#endif /* LMB204BFC_LCDMODULE_USED. */

#endif /* BOARD_1_0_USED */
